﻿using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

using Basteroid;

namespace CSharpCLI
{

    public struct WD_UserInfo
    {
        // 来自ACCOUNT_INFO。

        public string accountInfoACCOUNT_ID;
        public string accountInfoBANK;// 开户行。
        public string accountInfoREGTIME;// 开户日期。
        public string accountInfoACC_TYPE;
        public string accountInfoPERSON;// 开户人。
        public string accountInfoUSEABLE;// 可用金额。
        public string accountInfoMONEY;// 余额。
        public string accountInfoPAYPASSWORD;// 支付密码。
        public string accountInfoSTATE;

        // 来自PERSON_INFO。
        public string personInfoID;
        public string personInfoNAME;
        public string personInfoSEX;
        public string personInfoENAME;
        public string personInfoBIRTHDAY;
        public string personInfoIDCARD;
        public string personInfoADDRESS;
        public string personInfoPHONE;
        public string personInfoEMAIL;
        public string personInfoPASSWORD;
        public string personInfoACCOUNT;
        public string personInfoREGTIME;
        public string personInfoCELLPHONE;
        public string personInfoSTATE;

        // 来自CERTIFICATES。
        public string certificatesC_ID;
        public string certificatesPUB_KEY;
        public string certificatesREG_TIME;
        public string certificatesPID;
        public string certificatesSTATE;
    }

    public class DataSource
    {
        GL_DataBase kiraBase = new GL_DataBase("Data Source=FANTANY;Initial Catalog=ec;User ID=testuser;Password=oki");

        // 通过用户ID获得公钥。
        public string CertificatePublicKey(string CerSN)
        {
            string gottenPublicKey = kiraBase.GL_SelectFrom("PUB_KEY", "CERTIFICATES", "C_ID", CerSN);
            return gottenPublicKey;
        }

        // 通过用户ID获得登录信息。
        public string AccountLogin(string AccountNum)
        {
            string userID = kiraBase.GL_SelectFrom("PERSON", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            string userName = kiraBase.GL_SelectFrom("NAME", "PERSON_INFO", "ID", userID);
            string userBalance = kiraBase.GL_SelectFrom("MONEY", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);

            if ((userName == "") || (userBalance == ""))
            {
                return "";
            }
            else
            {
                return userName + " " + userBalance;
            }
        }

        public WD_UserInfo GetUserInfo_ByAccountNum(string AccountNum)
        {
            WD_UserInfo userInfoToReturn;
            //string openPerson = kiraBase.GL_SelectFrom("NAME", "PERSON_INFO", "ACCOUNT", AccountNum);
            //userInfoToReturn.accountID = AccountNum;
            //userInfoToReturn.accountType = kiraBase.GL_SelectFrom("ACC_TYPE", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            //userInfoToReturn.openPerson = openPerson;
            //userInfoToReturn.openBank = kiraBase.GL_SelectFrom("BANK", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            //userInfoToReturn.openDate = kiraBase.GL_SelectFrom("REGTIME", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            //userInfoToReturn.balance = kiraBase.GL_SelectFrom("MONEY", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            //userInfoToReturn.balanceUsable = kiraBase.GL_SelectFrom("USEABLE", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            //userInfoToReturn.passwordToPay = kiraBase.GL_SelectFrom("PAYPASSWORD", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);

            userInfoToReturn.accountInfoACCOUNT_ID = AccountNum;
            userInfoToReturn.accountInfoBANK = kiraBase.GL_SelectFrom("BANK", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            userInfoToReturn.accountInfoREGTIME = kiraBase.GL_SelectFrom("REGTIME", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            userInfoToReturn.accountInfoACC_TYPE = kiraBase.GL_SelectFrom("ACC_TYPE", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            userInfoToReturn.accountInfoPERSON = kiraBase.GL_SelectFrom("PERSON", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            userInfoToReturn.accountInfoUSEABLE = kiraBase.GL_SelectFrom("USEABLE", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            userInfoToReturn.accountInfoMONEY = kiraBase.GL_SelectFrom("MONEY", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            userInfoToReturn.accountInfoPAYPASSWORD = kiraBase.GL_SelectFrom("PAYPASSWORD", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);
            userInfoToReturn.accountInfoSTATE = kiraBase.GL_SelectFrom("STATE", "ACCOUNT_INFO", "ACCOUNT_ID", AccountNum);

            string personID = userInfoToReturn.accountInfoPERSON;

            userInfoToReturn.personInfoID = personID;
            userInfoToReturn.personInfoNAME = kiraBase.GL_SelectFrom("NAME", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoSEX = kiraBase.GL_SelectFrom("SEX", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoENAME = kiraBase.GL_SelectFrom("ENAME", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoBIRTHDAY = kiraBase.GL_SelectFrom("BIRTHDAY", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoIDCARD = kiraBase.GL_SelectFrom("IDCARD", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoADDRESS = kiraBase.GL_SelectFrom("ADDRESS", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoPHONE = kiraBase.GL_SelectFrom("PHONE", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoEMAIL = kiraBase.GL_SelectFrom("EMAIL", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoPASSWORD = kiraBase.GL_SelectFrom("PASSWORD", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoACCOUNT = kiraBase.GL_SelectFrom("ACCOUNT", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoREGTIME = kiraBase.GL_SelectFrom("REGTIME", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoCELLPHONE = kiraBase.GL_SelectFrom("CELLPHONE", "PERSON_INFO", "ID", personID);
            userInfoToReturn.personInfoSTATE = kiraBase.GL_SelectFrom("STATE", "PERSON_INFO", "ID", personID);

            userInfoToReturn.certificatesC_ID = kiraBase.GL_SelectFrom("C_ID", "CERTIFICATES", "PID", personID);
            userInfoToReturn.certificatesPUB_KEY = kiraBase.GL_SelectFrom("PUB_KEY", "CERTIFICATES", "PID", personID);
            userInfoToReturn.certificatesREG_TIME = kiraBase.GL_SelectFrom("REG_TIME", "CERTIFICATES", "PID", personID);
            userInfoToReturn.certificatesPID = personID;
            userInfoToReturn.certificatesSTATE = kiraBase.GL_SelectFrom("STATE", "CERTIFICATES", "PID", personID);

            return userInfoToReturn;
        }

        // 分配新的支票号。
        public string AllocNewChqSN()
        {
            string newChequeID = "1";
            newChequeID += DateTime.Now.ToString("hhmmss");

            int i_LinesEffected = kiraBase.GL_InsertInto("CHEQUE_INFO", "CHEQUE_ID, MONEY", newChequeID + ", 0");
            if (i_LinesEffected == 0)
            {
                return "";
            }

            return newChequeID;
        }

        // 看这个SN的支票存不存在，true表示存在。
        public bool ChqSN(string TransChqSN)
        {
            string chqSNGotten = kiraBase.GL_SelectFrom("CHEQUE_ID", "CHEQUE_INFO", "CHEQUE_ID", TransChqSN);

            if (chqSNGotten == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        // 成功返回true，失败返回false。
        public bool Transfer(string sourceAccID, string destiAccID, double amount)
        {
            string previousSourceAmount = kiraBase.GL_SelectFrom("MONEY", "ACCOUNT_INFO", "ACCOUNT_ID", sourceAccID);
            if (previousSourceAmount == "")
            {
                return false;
            }

            double previousSourceAmount_ByDouble = Convert.ToDouble(previousSourceAmount);

            if (previousSourceAmount_ByDouble < amount)
            {
                return false;
            }

            string previousDestiAmount = kiraBase.GL_SelectFrom("MONEY", "ACCOUNT_INFO", "ACCOUNT_ID", destiAccID);
            if (previousDestiAmount == "")
            {
                return false;
            }

            double previousDestiAmount_ByDouble = Convert.ToDouble(previousDestiAmount);

            kiraBase.GL_UpdateInto("ACCOUNT_INFO", "MONEY", Convert.ToString(previousSourceAmount_ByDouble - amount), "ACCOUNT_ID", sourceAccID);
            kiraBase.GL_UpdateInto("ACCOUNT_INFO", "MONEY", Convert.ToString(previousDestiAmount_ByDouble + amount), "ACCOUNT_ID", destiAccID);

            kiraBase.GL_UpdateInto("ACCOUNT_INFO", "USEABLE", Convert.ToString(previousSourceAmount_ByDouble - amount), "ACCOUNT_ID", sourceAccID);
            kiraBase.GL_UpdateInto("ACCOUNT_INFO", "USEABLE", Convert.ToString(previousDestiAmount_ByDouble + amount), "ACCOUNT_ID", destiAccID);

            kiraBase.GL_InsertInto("TRANS_DETAIL", 
                "ACCOUNT, MONEY, PLACE, REASON, DATE, WHEREDO, STATE", 
                sourceAccID + ", " 
                + Convert.ToString(amount) + ", "
                +"'Network', "
                +"'Cheque fair', "
                +DateTime.Now.ToString("yy-MM-dd")
                + "'To user.', "
                +"True");

            kiraBase.GL_InsertInto("TRANS_DETAIL",
                "ACCOUNT, MONEY, PLACE, REASON, DATE, WHEREDO, STATE", 
                destiAccID + ", "
                + Convert.ToString(amount) + ", "
                + "'Network', "
                + "'Cheque fair', "
                + DateTime.Now.ToString("yy-MM-dd")
                + "'To user.', "
                + "True");

            return true;
        }
    }
}